package org.javaside.giveme.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.javaside.giveme.core.WeiboConfig;
import org.javaside.giveme.vo.GiveMeContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 把ContextPath放入modle中.
 * 
 * @author xiaozhou
 * 
 */
public class ServletContextInterceptor extends HandlerInterceptorAdapter {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private WeiboConfig weiboConfig;
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (modelAndView != null) {
			GiveMeContext gmctx = (GiveMeContext)request.getSession().getAttribute("gmctx");
			if(gmctx==null){
				gmctx = new GiveMeContext();
				gmctx.setCtx(request.getContextPath());
				request.getSession().setAttribute("gmctx", gmctx);
				logger.debug("ctx:{}",request.getContextPath());
			}
		}
		

		System.setProperty("weibo4j.oauth.consumerKey", weiboConfig.getSinaConsumerKey());
		System.setProperty("weibo4j.oauth.consumerSecret",weiboConfig.getSinaConsumerSecret());
	}
}
